<?php

class ZFDulcet_Boxes_Box {

	public static $_gridInPage = 0;
	private $_boxAlternate = false;
	private $_boxId = 'box';
	private $_boxLabel;
	private $_boxType = '12';
	private $_buttonHeader = array();
	private $_itens = array();

	public function __construct($label, $id = null) {
		$this->_boxLabel = $label;

		if (null === $id) {
			$id = 'box_' . preg_replace('#[^a-z0-9]#i', '', $label);
		}
		$this->_boxId = $id;
	}

	public function addButtonHeader(ZFDulcet_Boxes_Button_Header $button) {
		$this->_buttonHeader[] = $button;
	}

	public function addForm(Zend_Form $form) {
		$this->addItem($form->getId(), $form);
	}

	public function addGrid(Bvb_Grid $grid) {
		if (self::$_gridInPage == 0) {
			Zend_Layout::getMvcInstance()->getView()->headScript()->appendFile(ZFDULCET_LIB_DIR_PLUGINS . '/datatables/media/js/jquery.dataTables.js');
			Zend_Layout::getMvcInstance()->getView()->headScript()->appendScript('$(document).ready(function() {
	$.fn.dataTableExt.sErrMode = \'throw\';
	$(\'.isdatatable\').dataTable({
		"bPaginate": false,
		"bFilter": false,
		"bInfo": false,
		"bSort": false,
		"oLanguage": {
			"sProcessing": "Processando...",
			"sLengthMenu": "Mostrar _MENU_ registros",
			"sZeroRecords": "Não foram encontrados resultados",
			"sInfo": "Mostrando de _START_ até _END_ de _TOTAL_ registros",
			"sInfoEmpty": "Mostrando de 0 até 0 de 0 registros",
			"sInfoFiltered": "(filtrado de _MAX_ registros no total)",
			"sInfoPostFix": "",
			"sSearch":"Buscar:",
			"sUrl": "",
			"oPaginate": {
				"sFirst": "Primeiro",
				"sPrevious": "Anterior",
				"sNext": "Seguinte",
				"sLast": "Último"
			}
		}
	});
});');
		}

		$this->addItem($grid->getNameGrid(), $grid);
		self::$_gridInPage++;
	}

	protected function addItem($key, $value) {
		$this->_itens[$key] = $value;
	}

	public function addPart($partName, $partValues = array()) {
		$this->addItem($partName, array(
			'name' => $partName,
			'type' => 'part',
			'values' => $partValues,
		));
	}

	public function getButtonHeadersCount() {
		return count($this->_buttonHeader);
	}

	public function getButtonHeaders() {
		return $this->_buttonHeader;
	}

	public function getBoxAlternate() {
		return $this->_boxAlternate;
	}

	public function getBoxId() {
		return $this->_boxId;
	}

	public function getBoxLabel() {
		return $this->_boxLabel;
	}

	public function getBoxType() {
		return $this->_boxType;
	}

	public function getForms() {
		return $this->_itens;
	}

	public function getItens() {
		return $this->_itens;
	}

	public function getParts() {
		return $this->_itens;
	}

	public function setBoxAlternate($alternate = true) {
		$this->_boxAlternate = $alternate;
	}

	public function setBoxType($type) {
		if (($type < 0) or ($type > 12)) {
			return;
		}
		$this->_boxType = $type;
	}

}